package com.example.petstore.controller;

import com.example.petstore.common.CommonResponse;
import com.example.petstore.domain.Category;
import com.example.petstore.domain.Item;
import com.example.petstore.domain.Product;
import com.example.petstore.mapper.ProductMapper;
import com.example.petstore.service.ItemService;
import com.example.petstore.service.ProductService;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@SessionAttributes({"loginAdmin"})
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private ItemService itemService;
    @GetMapping("/getProductListByCategory")
    @ResponseBody
    public CommonResponse getProductListByCategory(String category){
        return productService.getProductListByCategory(category);
    }

    @GetMapping("/getProductListByKey")
    @ResponseBody
    public CommonResponse getProductListByKey(String keyWord){
        return productService.getProductListByKey(keyWord);
    }

    @GetMapping("/admin/getProductListByKeyForJS")
    @ResponseBody
    public CommonResponse getProductListByKeyForJS(String keyword){
        return productService.getProductListByKey(keyword);

    }
    @GetMapping("/admin/deleteProductById")
    @ResponseBody
    public CommonResponse deleteProductById(String productId){
        List<Item> itemList = itemService.getItemListByProductId(productId).getData();
        if (!itemList.isEmpty()) {
            return CommonResponse.creatForError("请先删除对应的条目！");
        }else {
            return productService.deleteProductById(productId);
        }
    }
    @GetMapping("/admin/deleteItemById")
    @ResponseBody
    public CommonResponse deleteItemById(String itemId){
        return itemService.deleteItemByItemId(itemId);
    }
    @ResponseBody
    @PostMapping("/productEditor")/*test ok*/
    public CommonResponse itemEditor(@RequestParam("pic") MultipartFile pic, @RequestParam("textdescn") String textdescn,@RequestParam("productId") String productId){
        System.out.println(textdescn);
        System.out.println(pic);
        String descn = productService.handleFileUpload(pic,productId);
        System.out.println(descn);
        System.out.println(descn);
        if(descn != null) {
            Product product = new Product();
            product.setDescn(descn);
            product.setTextdescn(textdescn);
            return productService.updateProductByProductId(productId,product);
        } else {
            return CommonResponse.creatForSuccess("修改失败！");

        }
    }
//    public CommonResponse deleteAdmin(@RequestBody String jsonData) {
//        try {
//            // 解析 JSON 字符串
//            JsonNode jsonNode = objectMapper.readTree(jsonData);
//            System.out.println(jsonNode);
//            String adminId = jsonNode.get("adminId").asText();
//            System.out.println(adminId);
//            return adminService.deleteAdminById(adminId);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return CommonResponse.creatForError();
//        }



}
